package com.fjrz.bbxwj.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.VideoCateList
import razerdp.basepopup.BasePopupWindow


class VideoCategoryPopup(
    context: Context,
    val onConfirmTotal: (view: View, position: Int, videoCate: VideoCateList) -> Unit,
    val onConfirmDefault: (view: View, position: Int, videoCate: VideoCateList) -> Unit
) : BasePopupWindow(context) {

    init {
        setAdjustInputMethod(true)
        height = ScreenUtils.getAppScreenHeight() / 5 * 4
        popupGravity = Gravity.BOTTOM
        setAutoShowInputMethod(false)
        setOverlayNavigationBar(false)
        setBlurBackgroundEnable(true)
    }

    private val cateDefaultAdapter =
        object : BaseQuickAdapter<VideoCateList, BaseViewHolder>(R.layout.item_video_cate_label) {
            init {
                addChildClickViewIds(R.id.ivVideoCateLabelDelete)
            }

            override fun convert(holder: BaseViewHolder, item: VideoCateList) {
                holder.setText(R.id.tvVideoCateLabelName, item.name)
                    .setGone(R.id.ivVideoCateLabelDelete, (item.id == -1 || item.id == -2))
            }
        }

    private val cateTotalAdapter =
        object : BaseQuickAdapter<VideoCateList, BaseViewHolder>(R.layout.item_video_cate_label) {
            override fun convert(holder: BaseViewHolder, item: VideoCateList) {
                holder.setText(R.id.tvVideoCateLabelName, item.name)
                    .setGone(R.id.ivVideoCateLabelDelete, true)
            }
        }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_video_category)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivVideoCateClose).setOnClickListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 400L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 400L }
    }

    fun setFillDataCateListDefault(
        cateListDefault: MutableList<VideoCateList>
    ): VideoCategoryPopup {
        contentView.findViewById<RecyclerView>(R.id.rvVideoCateDefault).adapter =
            cateDefaultAdapter.apply {
                setNewInstance(cateListDefault)
            }
        cateDefaultAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id != R.id.ivVideoCateLabelDelete)
                return@setOnItemChildClickListener
            val itemData: VideoCateList = cateDefaultAdapter.getItem(position)
            if (itemData.id == -1)
                return@setOnItemChildClickListener
            if (itemData.id == -2)
                return@setOnItemChildClickListener
            onConfirmTotal(view, position, itemData)
        }
        return this
    }

    fun setFillDataCateListTotal(
        cateListTotal: MutableList<VideoCateList>
    ): VideoCategoryPopup {
        contentView.findViewById<RecyclerView>(R.id.rvVideoCateTotal).adapter =
            cateTotalAdapter.apply {
                setNewInstance(cateListTotal)
            }
        cateTotalAdapter.setOnItemClickListener { _, view, position ->
            onConfirmDefault(view, position, cateTotalAdapter.getItem(position))
        }
        return this
    }

}